"""
time: 2024/10/30
title: DateTime
message:
日期/时间工具
"""
import time
import datetime


class DateTime:
    def __init__(self):
        pass

    def date_to_num(self, date, for_time="%Y-%m-%d %S:%m:%d:%ms"):
        """
        日期转为数字
        :param date:
        :param for_time:
        :return:
        """
        pass

    @staticmethod
    def num_to_date(num, for_time='%Y-%m-%d %H:%M:%S'):
        """
        数字转为时间
        :param num:
        :param for_time:
        :return:
        """
        localtime = time.localtime(num / 1000.0)
        value = time.strftime(for_time, localtime)
        print(value)

    @staticmethod
    def get_now_time():
        # 获取当前时间戳，精确到秒，然后转为毫秒
        current_time_ms = int(round(time.time() * 1000))
        print("当前时间戳（毫秒）:", current_time_ms)
        return current_time_ms

    @staticmethod
    def time_difference(start, end=None, for_mat="%Y-%m-%d %H:%M:%S"):
        """
        计算两个时间时间差
        :param start:
        :param end:
        :param for_mat:
        :return:
        """
        if end is None:
            end = datetime.datetime.now().strftime(for_mat)
        if start is None or start.strip() == "":
            return {"D": 0, "H": 0, "M": 0, "S": 0}
        data1 = datetime.datetime.strptime(start, for_mat)
        data2 = datetime.datetime.strptime(end, for_mat)
        date = data1 - data2
        return {"D": date.days, "H": date.seconds // 3600, "M": (date.seconds // 60) % 60, "S": date.seconds}

    @staticmethod
    def get_now_time_str(for_mat="%Y-%m-%d %H:%M:%S"):
        """
        获取当前时间
        :param for_mat:
        :return:
        """
        return datetime.datetime.now().strftime(for_mat)


if __name__ == '__main__':
    DateTime().num_to_date(1655860780000)
    DateTime().num_to_date(1675860780000)
    # 1675860780000
    # 1730278875316
    DateTime().get_now_time()
